

//
//  TableViewController.swift
//  Swift 3.0 基础语法
//
//  Created by YYY on 2017/7/17.
//  Copyright © 2017年 成品家（北京）网路科技有限公司. All rights reserved.
//

import UIKit

let identify = "cellID"

class TableViewController: UIViewController, UITableViewDataSource,UITableViewDelegate {

    var array1: [String]?
    
    override func viewDidLoad()
    {
        super.viewDidLoad()

        title = "主视图"
        let dev = Developer(name: "zhouni", age: 10)
        print(dev?.name)
       print(dev?.age)
        //demo5()
        
        array1  = ["数组语法","字典语法","字符串语法","UIControls","Function函数","闭包","Block","GCD","URL","getter setter"]
        
        //创建UITableView
        setUpUI()
        // Do any additional setup after loading the view.
    }

    //MARK: - 重载函数
    func demo1()
    {
        //Swift中，默认一个项目中，所有的类都是共享的，可以直接访问，不需要import
        
        //Swift中，()相当于OC中的alloc、init
//        let s = Student()
//        //print("\(s.number) ("---")   \(s.name)")
//        
//        print(s.number)
//        print(s.name)
//        print(s.number,"---",s.name)
        let s = Student(name: "wangmin", number: "1001")
        print(s.number,s.name)
    }
    
    //MARK: - 重写函数
    func demo2()
    {
        let p = Person(name: "老王")
        print(p.name)
    }
    //MARK: - KVC
    func demo3()
    {
        let boss = Boss(dict: ["name": "王晶" as AnyObject,"age": 100 as AnyObject,"title": "wuye" as AnyObject])
        print(boss)
        print("\(boss.name)   \(boss.age)")
        
    }
    func demo4()
    {
        let manager = Manager(dict: ["name": "王晶" as AnyObject,"age": 100 as AnyObject,"title": "wuye" as AnyObject,"wage": "2000" as AnyObject,"test" : "abc" as AnyObject])
        print("\(manager.name)   \(manager.age)  \(manager.title)  \(manager.wage)")
    }
    
    func demo5()
    {
        print(Softare.propertyList())
    }
    //创建UITableView
    func setUpUI()
    {
        let myTableView = UITableView(frame: view.bounds, style: .plain)
        view.addSubview(myTableView)
        myTableView.delegate = self
        myTableView.dataSource = self
        myTableView.register(UITableViewCell.self, forCellReuseIdentifier: identify)
        //创建表头
//        let headerView = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 50))
//        myTableView.tableHeaderView = headerView
    }
    
    //行数
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return (array1?.count)!
    }
    //创建cell
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: identify, for: indexPath)
        
        cell.textLabel?.text = "\((array1?[indexPath.row])!)"
        cell.textLabel?.font = UIFont(name: "Arial", size: 16)
        cell.selectionStyle = .none
        return cell
    }
    //选中表视图
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    {
        switch indexPath.row {
        case 0:
            let arrayVC = ArrayViewController()
            pushVC(controlsVC: arrayVC, indexPath: indexPath)
        case 1:
            let dictionaryVC = DictionaryViewController()
            pushVC(controlsVC: dictionaryVC, indexPath: indexPath)
        case 2:
            let stringVC = StringViewController()
            pushVC(controlsVC: stringVC, indexPath: indexPath)
        case 3:
            let controlsVC = ControlsViewController()
            pushVC(controlsVC: controlsVC, indexPath: indexPath)
            //present(controlsVC, animated: true, completion: nil)
        case 4:
            let functionVC = FunctionViewController()
            pushVC(controlsVC: functionVC, indexPath: indexPath)
        case 5:
            let closureVC = ClosureViewController()
            pushVC(controlsVC: closureVC, indexPath: indexPath)
        case 6:
            let blockVC = BlockViewController()
            pushVC(controlsVC: blockVC, indexPath: indexPath)
        case 7:
            let gcdVC = GCDViewController()
            pushVC(controlsVC: gcdVC, indexPath: indexPath)
        case 8:
            let urlVC = URLViewController()
            pushVC(controlsVC: urlVC, indexPath: indexPath)
        case 9:
            let getSetVC = GetSetViewController()
            pushVC(controlsVC: getSetVC, indexPath: indexPath)
        default:
            break
        }
    }
    
    func pushVC(controlsVC: UIViewController,indexPath: IndexPath)
    {
        controlsVC.title = (array1?[indexPath.row])!
        navigationController?.pushViewController(controlsVC, animated: true)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
